<?php
declare (strict_types = 1);

namespace app\admin\controller\erp;

use app\admin\controller\AuthController;
use app\admin\model\erp\ErpGoods;
use app\admin\model\erp\ErpGoodsTypes;
use app\admin\model\system\SystemMenus;
use think\facade\Db;
use think\Request;

class Reserve extends AuthController
{
    /**
     * 显示列表
     *
     * @param  \think\Request  $req
     * @return \think\Response
     */
    public function index(Request $req)
    {
        if ($req->isAjax()) {
            $data = [];
            $op = $req->param('op','ok');
            if($op == 'sub') {
                $gid = $req->param('id/d', 0);
                $data['data'] = Db::name('erp_storage')->alias('a')
                    ->leftJoin('erp_reserve b', 'b.gid = '.$gid.' AND b.sid = a.id')
                    ->field('a.name,IFNULL(b.stock,0) as stock')
                    ->select()->toArray();
                foreach ($data['data'] as &$val) {

                }
                $data['count'] = 0;
                //echo Db::getLastSql();
            } else {
                $page = $req->param('page/d', 1);
                $limit = $req->param('limit/d', 15);
                $query = ErpGoods::order('id DESC');
                // 条件搜索
                $code = $req->param('code');
                if (!is_null($code) && strlen($code) && $code != 0) {
                    $query->where('code','like',"{$code}%");
                }
                $name = $req->param('name');
                if (!is_null($name) && strlen($name)) {
                    $query->where('name','like',"%$name%");
                }

                $data['count'] = $query->count('id');
                $data['data'] = $query->field('id,agent_price,agent_price_limit,code,max_stock,min_stock,name,norm,price,retail_price,retail_price_limit,stock,tid')->page($page,$limit)->select()->toArray();
            }
            $data['code'] = 0;
            $data['msg'] = '';
            return json($data);
        }
        $sonAuth = SystemMenus::getSonAuth($req->controller(), $req->action(),array_keys($this->auth));

        $treeData = ErpGoodsTypes::typeAll(1);
        // 显示页面
        $this->assign('sonAuth', $sonAuth);
        $this->assign('treeData', $treeData);
        return $this->fetch();
    }

    /**
     * 创建资源.
     *
     * @param  \think\Request  $req
     * @return \think\Response
     */
    public function add(Request $req)
    {
        //
    }

    /**
     * 更新资源
     *
     * @param  \think\Request  $req
     * @return \think\Response
     */
    public function edit(Request $req)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
